﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace EasySL.Controls
{
    public partial class ExDataGrid : DataGrid
    {
        private void BindFilterEvent()
        {
            this.ExFilterMenu.OnClickItem += (action, bindProperty) =>
            {
                this.FilterMenuPopup.IsOpen = false;
               

                if (action == ExFilterAction.Top10)
                {
                    this.SetFilterState(bindProperty, action, null);
                    this.Filter();
                    return;
                }

                if (action == ExFilterAction.AboveAverage)
                {
                    this.SetFilterState(bindProperty, action, null);
                    this.Filter();
                    return;
                }

                if (action == ExFilterAction.BelowAverage)
                {
                    this.SetFilterState(bindProperty, action, null);
                    this.Filter();
                    return;
                }

                if (action == ExFilterAction.Clear)
                {
                    this.SetFilterState(bindProperty, ExFilterAction.NULL, null);
                    this.Filter();
                    return;
                }

                filterPanel.Show();
                filterPanel.SetFilterPanel(bindProperty, action);

                this.filterPanel.OnSubmit = null;
                this.filterPanel.OnSubmit += (property, value) =>
                {
                    this.SetFilterState(property, action, value);
                    this.Filter();
                };

            };                       
        }

        private void Filter()
        {
            currentDataRows = this.dataTable.AsEnumerable();

            foreach (ExDataGridColumn c in this.Columns)
            {
                string bindProperty = c.BindProperty;
                double averageValue = 0;

                switch (c.CurrentFilterAction)
                {
                    case ExFilterAction.Top10:
                        currentDataRows = this.currentDataRows.OrderByDescending(r => r[bindProperty]).Take(10).ToList();
                        break;

                    case ExFilterAction.AboveAverage:
                        averageValue = currentDataRows.Average(r => Convert.ToDouble(r[bindProperty]));
                        currentDataRows = this.currentDataRows.Where(r => Convert.ToDouble(r[bindProperty]) > averageValue).ToList();
                        break;

                    case ExFilterAction.BelowAverage:
                        averageValue = currentDataRows.Average(r => Convert.ToDouble(r[bindProperty]));
                        currentDataRows = this.currentDataRows.Where(r => Convert.ToDouble(r[bindProperty]) < averageValue).ToList();
                        break;

                    case ExFilterAction.GreaterThan:
                        currentDataRows = this.currentDataRows.Where(r => Convert.ToDouble(r[bindProperty]) > Convert.ToDouble(c.CurrentFilterValue.Value1)).ToList();
                        break;

                    case ExFilterAction.LessThan:
                        currentDataRows = this.currentDataRows.Where(r => Convert.ToDouble(r[bindProperty]) < Convert.ToDouble(c.CurrentFilterValue.Value1)).ToList();
                        break;

                    case ExFilterAction.Between:
                        currentDataRows = this.currentDataRows.Where(r => Convert.ToDouble(r[bindProperty]) > Convert.ToDouble(c.CurrentFilterValue.Value1) && Convert.ToDouble(r[bindProperty]) < Convert.ToDouble(c.CurrentFilterValue.Value2)).ToList();
                        break;

                    case ExFilterAction.Equals:
                        currentDataRows = this.currentDataRows.Where(r => string.Equals(r[bindProperty].ToString(), c.CurrentFilterValue.Value1.ToString(), StringComparison.OrdinalIgnoreCase)).ToList();
                        break;

                    case ExFilterAction.NotEquals:
                        currentDataRows = this.currentDataRows.Where(r => !string.Equals(r[bindProperty].ToString(), c.CurrentFilterValue.Value1.ToString(), StringComparison.OrdinalIgnoreCase)).ToList();
                        break;

                    case ExFilterAction.BeginWith:
                        currentDataRows = this.currentDataRows.Where(r => r[bindProperty].ToString().StartsWith(c.CurrentFilterValue.Value1.ToString(), StringComparison.OrdinalIgnoreCase)).ToList();
                        break;

                    case ExFilterAction.EndWith:
                        currentDataRows = this.currentDataRows.Where(r => r[bindProperty].ToString().EndsWith(c.CurrentFilterValue.Value1.ToString(), StringComparison.OrdinalIgnoreCase)).ToList();
                        break;

                    case ExFilterAction.Contains:
                        currentDataRows = this.currentDataRows.Where(r => r[bindProperty].ToString().ToLower().Contains(c.CurrentFilterValue.Value1.ToString().ToLower())).ToList();
                        break;

                    case ExFilterAction.NotContains:
                        currentDataRows = this.currentDataRows.Where(r => !r[bindProperty].ToString().ToLower().Contains(c.CurrentFilterValue.Value1.ToString().ToLower())).ToList();
                        break;
                }
            }

            if (!string.IsNullOrEmpty(this.SortProperty))
            {
                if (this.IsAscend)
                    currentDataRows = this.currentDataRows.OrderBy(r => r[this.SortProperty]);
                else
                    currentDataRows = this.currentDataRows.OrderByDescending(r => r[this.SortProperty]);
            }

            this.SetDataSource(currentDataRows.Take(PageSize));
            this.UpdateSummayRow();
            this.SetCheckAll();
            this.SetScrollBar();
            this.SetRowNumbers(1);
        }

        private void SetFilterState(string property, ExFilterAction action, ExFilterValue value)
        {
            foreach (ExDataGridColumn c in this.Columns)
            {
                if (c != null && c.BindProperty == property)
                {
                    c.CurrentFilterAction = action;
                    c.CurrentFilterValue = value;
                    break;
                }
            }

            foreach (var panel in this.CommandPanels)
                if (panel.BindProperty == property)
                    panel.IsFilter = action != ExFilterAction.NULL;
        }
    }
}
